# -*- coding: utf-8 -*-
"""
调试数据提取问题
检查承受租金和建筑面积字段的提取逻辑
"""

import asyncio
import logging
import re
from src.crawler.crawler_engine import CrawlerEngine


async def debug_data_extraction():
    """调试数据提取"""
    
    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger(__name__)
    
    crawler = CrawlerEngine()
    
    try:
        # 启动爬虫
        await crawler.start(headless=True)
        
        # 导航到页面
        target_url = "https://sz.pupuwang.com/siting/list?industryCode=11"
        success = await crawler.browser_manager.navigate_to(target_url)
        
        if not success:
            print("❌ 页面导航失败")
            return
        
        # 等待页面加载
        await crawler.browser_manager.page.wait_for_load_state('networkidle', timeout=15000)
        
        # 使用精确XPath获取列表项
        precise_xpath = '/html/body/div[1]/div/div[2]/div[3]/div[1]/ul[1]/li'
        items_locator = crawler.browser_manager.page.locator('xpath=' + precise_xpath)
        item_count = await items_locator.count()
        
        print(f"🔍 找到 {item_count} 条数据")
        
        if item_count > 0:
            # 获取第一条数据的完整文本
            first_item = items_locator.nth(0)
            full_text = await first_item.text_content()
            
            print(f"\n📋 第一条数据的完整文本:")
            print(f"'{full_text}'")
            print(f"文本长度: {len(full_text)} 字符")
            
            # 清理文本
            clean_text = full_text.replace('\xa0', ' ').replace('\u2000', ' ').strip()
            print(f"\n🧹 清理后的文本:")
            print(f"'{clean_text}'")
            
            # 测试租金提取
            print(f"\n💰 租金提取测试:")
            rent_patterns = [
                r'(\d+[-~–—]\d+元/月)',  # 范围租金
                r'(\d+[-~–—]\d+万/月)',  # 范围租金（万）
                r'(\d+元/月)',          # 固定租金
                r'(\d+万/月)',          # 固定租金（万）
                r'(面议)',              # 面议
                r'(租金面议)',          # 租金面议
                r'(\d+[-~–—]\d+)',      # 纯数字范围
                r'(\d+)',               # 纯数字
            ]
            
            for i, pattern in enumerate(rent_patterns):
                match = re.search(pattern, clean_text)
                if match:
                    print(f"  ✅ 模式{i+1}: {pattern} -> 匹配: '{match.group(1)}'")
                    break
            else:
                print(f"  ❌ 没有匹配到任何租金模式")
            
            # 测试面积提取
            print(f"\n📏 面积提取测试:")
            area_patterns = [
                r'(\d+[-~]\d+)\s*平米?',
                r'(\d+)\s*平米?',
                r'(\d+[-~]\d+)\s*㎡',
                r'(\d+)\s*㎡'
            ]
            
            for i, pattern in enumerate(area_patterns):
                match = re.search(pattern, clean_text)
                if match:
                    print(f"  ✅ 模式{i+1}: {pattern} -> 匹配: '{match.group(0)}'")
                    break
            else:
                print(f"  ❌ 没有匹配到任何面积模式")
            
            # 测试区域提取
            print(f"\n🗺️ 区域提取测试:")
            areas = ['罗湖区', '福田区', '南山区', '宝安区', '龙岗区', '盐田区', '龙华区', '坪山区', '光明区', '大鹏新区']
            found_areas = []
            for area in areas:
                if area in clean_text:
                    found_areas.append(area)
            
            if found_areas:
                print(f"  ✅ 找到区域: {', '.join(found_areas)}")
            else:
                print(f"  ❌ 没有找到任何区域")
            
            # 分析文本结构
            print(f"\n🔍 文本结构分析:")
            lines = clean_text.split('\n')
            for i, line in enumerate(lines):
                if line.strip():
                    print(f"  行{i+1}: '{line.strip()}'")
            
            # 查找所有数字模式
            print(f"\n🔢 所有数字模式:")
            number_patterns = re.findall(r'\d+[-~–—]?\d*', clean_text)
            for i, num in enumerate(number_patterns):
                print(f"  数字{i+1}: '{num}'")
            
            # 查找包含"元"的文本
            print(f"\n💴 包含'元'的文本:")
            yuan_matches = re.findall(r'[^。，\n]*元[^。，\n]*', clean_text)
            for i, match in enumerate(yuan_matches):
                print(f"  元文本{i+1}: '{match.strip()}'")
            
            # 查找包含"平米"或"㎡"的文本
            print(f"\n📐 包含面积单位的文本:")
            area_matches = re.findall(r'[^。，\n]*(?:平米?|㎡)[^。，\n]*', clean_text)
            for i, match in enumerate(area_matches):
                print(f"  面积文本{i+1}: '{match.strip()}'")
        
    except Exception as e:
        print(f"❌ 调试失败: {e}")
        import traceback
        traceback.print_exc()
    
    finally:
        await crawler.stop()


if __name__ == "__main__":
    asyncio.run(debug_data_extraction())
